package com.yuju.controller.accounts;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.model.account.AccountModel;
import com.yuju.util.Utils;
import com.yuju.account.enums.AccountReturnCodeEnum;
import com.yuju.account.service.IAccountService;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.vo.account.AccountVO;

/**
 * 类名：AccountsController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年11月25日 下午6:38:35  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("account")
public class AccountsController extends BaseController<AccountVO, AccountModel> {
	
	@Autowired
	private IAccountService accountService;
	
	@RequestMapping("getCTMoneySum")
	@ResponseBody
	public Result getCTMoneySum(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		BigDecimal sum = accountService.getCTMoneySum(convert(accountVO));
		Result result = new Result();
		if(Utils.isNotNull(sum)){
			result.setData(sum);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.CTSUM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.CTSUM_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.CTSUM_QUERY_FAILURE.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.CTSUM_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("getCTReport")
	@ResponseBody
	public Result getCTReport(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		PageModel pageModel = null;
		List<AccountModel> list = null;
		if(Utils.isNotNull(accountVO.getPage())){
			pageModel = accountService.getCTReportPage(convert(accountVO), accountVO.getPage(), accountVO.getPageSize());
		}else{
			list = accountService.getCTReport(convert(accountVO));
		}
		Result result = new Result();
		if(Utils.isNotNull(pageModel)){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.CTREPORT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.CTREPORT_QUERY_SUCCESS.getDesc());
		}else if(Utils.isNotNull(list)){
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.CTREPORT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.CTREPORT_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.CTREPORT_QUERY_FAILURE.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.CTREPORT_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：获得账务总数量<br/>
	 *
	 * @author jiaokn
	 * @Date 2018年2月1日 上午12:00:12 <br/>
	 */
	@RequestMapping("getAccountAmountSum")
	@ResponseBody
	public Result getAccountAmountSum(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		if(Utils.isNotNull(obj.getJSONArray("businessTypes"))){
			accountVO.setBusinessTypes(null);
			JSONArray jsonArray = obj.getJSONArray("businessTypes");
			String jsonString = jsonArray.toJSONString();
			ArrayList<Byte> businessTypes = JSON.parseObject(jsonString, new TypeReference<ArrayList<Byte>>(){});
			accountVO.setBusinessTypes(businessTypes);
		}
		Integer sum = accountService.getAccountAmountSum(convert(accountVO));
		Result result = new Result();
		if(Utils.isNotNull(sum)){
			result.setData(sum);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.ACCOUNT_AMOUNTQUERY_SUCCESS.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.ACCOUNT_AMOUNTQUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.ACCOUNT_AMOUNTQUERY_FAILURE.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.ACCOUNT_AMOUNTQUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("getAccountSubreport")
	@ResponseBody
	public Result getAccountSubreport(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		if(Utils.isNotNull(obj.getJSONArray("businessTypes"))){
			accountVO.setBusinessTypes(null);
			JSONArray jsonArray = obj.getJSONArray("businessTypes");
			String jsonString = jsonArray.toJSONString();
			ArrayList<Byte> businessTypes = JSON.parseObject(jsonString, new TypeReference<ArrayList<Byte>>(){});
			accountVO.setBusinessTypes(businessTypes);
		}
		
		PageModel pageModel = null;
		List<AccountModel> list = null;
		if(Utils.isNotNull(accountVO.getPage())){
			pageModel = accountService.listOrderAccount(convert(accountVO), accountVO.getPage(), accountVO.getPageSize());
		}else{
			list = accountService.listOrderAccountNoPage(convert(accountVO));
		}
		Result result = new Result();
		if(Utils.isNotNull(pageModel)){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.ACCOUNTREPORT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.ACCOUNTREPORT_QUERY_SUCCESS.getDesc());
		}else if(Utils.isNotNull(list)){
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.ACCOUNTREPORT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.ACCOUNTREPORT_QUERY_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.ACCOUNTREPORT_QUERY_FAILURE.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.ACCOUNTREPORT_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：客账查询<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月19日 下午4:52:01 <br/>
	 */
	@RequestMapping("listOrderAccount")
	@ResponseBody
	public Result listOrderAccount(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		Map<String, String> fail = FieldValidator.validateAll(accountVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		PageModel pageModel = accountService.listOrderAccount(convert(accountVO), accountVO.getPage(), accountVO.getPageSize());
		Result result = new Result();
		if(Utils.isNotNull(pageModel) &&  pageModel.getTotal()>0){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.ACCOUNT_QUERY_SUCCESS.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.ACCOUNT_QUERY_SUCCESS.getDesc());
		}else if(Utils.isNotNull(pageModel) && (pageModel.getTotal()<=0 || Utils.isNotNull(pageModel.getTotal()))){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.ACCOUNT_NOTHING.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.ACCOUNT_NOTHING.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(AccountReturnCodeEnum.ACCOUNT_QUERY_FAILURE.getCode());
			result.setReturnMsg(AccountReturnCodeEnum.ACCOUNT_QUERY_FAILURE.getDesc());
		}
		return result;
	}
	
}
